package com.redteam.servlets;

import com.redteam.data.beans.CharterCustomer;
import com.redteam.data.beans.CharterPilot;
import com.redteam.data.beans.Instructor;
import com.redteam.data.beans.MaintenanceEngineer;
import com.redteam.data.beans.Person;
import com.redteam.data.beans.Pilot;
import com.redteam.data.beans.Student;
import com.redteam.models.AuthorizationModel;
import com.redteam.models.DAOAccessor;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Takes the user information and if it is valid logs them in.
 * @author Jeremy Reed
 * @author Hai Dang
 * @author Anthony Ferrari
 */
public class Login extends HttpServlet {

    Person person;
    HttpSession session;
    DAOAccessor daoa;

    /**
     * Default constructor for Login
     */
    public Login() {
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        person = null;
        daoa = new DAOAccessor();
        response.setContentType("text/html;charset=UTF-8");
        ServletContext context = getServletContext();
        RequestDispatcher dispatch;
        session = request.getSession(true);
        AuthorizationModel am = new AuthorizationModel(request.getParameter("username"), request.getParameter("password"));

        if (am.userExists()) {
            if (am.checkPassword()) {
                person = am.getUserType();
                System.out.println(person.toString());
            } else {
                request.setAttribute("Failure", "Username or password does not match.");
                dispatch = context.getRequestDispatcher("/jsp/login.jsp");
                dispatch.forward(request, response);
            }
        } else {
            request.setAttribute("Failure", "Username or password does not match.");
            dispatch = context.getRequestDispatcher("/jsp/login.jsp");
            dispatch.forward(request, response);
        }
        session.setAttribute("firstName", person.getFirstName());
        session.setAttribute("lastName", person.getLastName());
        session.setAttribute("loggedIn", true);
        chooseMenu(request, am);
        dispatch = context.getRequestDispatcher("/jsp/index.jsp");
        dispatch.forward(request, response);
    }

    private void chooseMenu(HttpServletRequest request, AuthorizationModel authmodel) {
        if (authmodel.getUserType().getDiscriminator().equals("student")) {
            session.setAttribute("menuChoice", "menus/studentmenu.jsp");
            Student student = (Student) daoa.getStudentByID(person.getId());
            session.setAttribute("user", student);
        } else if (authmodel.getUserType().getDiscriminator().equals("instructor")) {
            session.setAttribute("menuChoice", "menus/instructormenu.jsp");
            Instructor instructor = (Instructor) daoa.getInstructorByID(person.getId());
            session.setAttribute("user", instructor);
        } else if (authmodel.getUserType().getDiscriminator().equals("mechanic")) {
            session.setAttribute("menuChoice", "menus/maintenancemenu.jsp");
            MaintenanceEngineer mainteng = (MaintenanceEngineer) daoa.getMaintenanceEngineerByID(person.getId());
            session.setAttribute("user", mainteng);
            session.setAttribute("menuChoice", "menus/maintenancemenu.jsp");
        } else if (authmodel.getUserType().getDiscriminator().equals("charterCustomer")) {
            CharterCustomer charterCustomer = (CharterCustomer) daoa.getCharterCustomerByID(person.getId());
            session.setAttribute("user", charterCustomer);
            session.setAttribute("menuChoice", "menus/chartercustomermenu.jsp");
        } else if (authmodel.getUserType().getDiscriminator().equals("charterPilot")) {
            CharterPilot charterPilot = (CharterPilot) daoa.getCharterPilotByID(person.getId());
            session.setAttribute("user", charterPilot);
            session.setAttribute("menuChoice", "menus/charterpilotmenu.jsp");
        } else if (authmodel.getUserType().getDiscriminator().equals("pilot")) {
            Pilot pilot = (Pilot) daoa.getPilotByID(person.getId());
            session.setAttribute("user", pilot);
            session.setAttribute("menuChoice", "menus/pilotmenu.jsp");
        } else if (authmodel.getUserType().getDiscriminator().equals("reservationAgent")) {
            session.setAttribute("user", person);
            session.setAttribute("menuChoice", "menus/mastermenu.jsp");
        } else if (authmodel.getUserType().getDiscriminator().equals("administrator")) {
            session.setAttribute("user", person);
            session.setAttribute("menuChoice", "menus/mastermenu.jsp");
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,
            IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException,
            IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
